﻿; (function (window, undefined, $) {
    var Request = new Object();
    Request = GetRequest();
    $(document).ready(function () {
        var gridopt = {
            url: options.listUrl+"?id="+ Request["id"],
            colModel: [
                    { display: '编号', name: 'ProjectNo', width: "10%", sortable: true, hide: false, align: 'left', iskey: true },
                    { display: '项目名称', name: 'ProjectName', width: "36%", sortable: false, align: 'left' },
                    { display: '贷款总额', name: 'TotalLoan', width: "13%", sortable: false, align: 'left' },
                    { display: '年收益率', name: 'AnnualisedRates', width: "7%", sortable: false, align: 'left', process: processAnnualisedRates },
                    { display: '项目位置', name: 'ProjectLocation', width: "12%", sortable: false, align: 'left' },
                    { display: '操作', name: 'ID', width: "15%", sortable: false, align: 'center', process: processOp }
            ],
            sortname: "ID",
            sortorder: "ASC",
            title: false,
            rp: 15,
            usepager: true,
            showcheckbox: false
        };
        var xjgrid = new xjGrid("gridlist", gridopt);

        function processAnnualisedRates(value, cell) {
            var ops = [];
            ops.push(Math.round(value * 100), "%");
            return ops.join("");
        }

        function processOp(value, cell) {
            var ops = [];
            ops.push("&nbsp;<a title='编辑' href='javascript:;'  onclick=\"util.Edit('", value, "')\"><i class='fa fa-edit' ></i>编辑</a>");
            ops.push("&nbsp;&nbsp;<a title='删除' class='abtn' href='javascript:;'  onclick=\"util.Delete('", value, "','", cell[1], "')\"><i class='fa fa-trash-o' ></i>删除</a>");
            return ops.join("");
        }

        $("#formQuery").submit(function () {
            xjgrid.Query(this);
            return false;
        });
        $("#btnSearch").click(function () {
            $("#formQuery").submit();
        });
        $("#btnAdd").click(function (e) {
            var url = options.editUrl + "?status=1";
            location.href = url;
        });

        util.PetProject = function (id) {
            var url = "PetProject?id=" + id;
            location.href = url;
        };

        util.Edit = function (id) {
            var url = options.editUrl + "?id=" + id + "&status=1";
            location.href = url;
        };

        util.Audit = function (id) {
            var url = options.editUrl + "?id=" + id + "&status=2";
            location.href = url;
        };

        util.RepayPres = function (id) {
            var url = "/Project/RepayPres.html/" + id;
            location.href = url;
        };

        util.Delete = function (id, name) {
            if (!confirm("您确定要执行删除操作吗？")) {
                return false;
            }
            $.post(options.deleteUrl + "/" + id, { id: id },
                  function (res) {
                      if (res.Status > 0) {
                          alert("操作成功！");
                          xjgrid.Reload();
                      }
                      else {
                          alert("操作失败！");
                      }
                  },
                  "json"
            );
        };
    });

})(window, undefined, jQuery);

function GetRequest() {
    var url = location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}